package com.lv.suyiyong.hfr.adapter

import android.view.View
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.lv.common.base.DataBindBaseViewHolder
import com.lv.common.imp.OnPositionTListener
import com.lv.common.ktx.LoadSrc
import com.lv.common.ktx.urlAddBorder
import com.lv.common.utils.MoneyUtil
import com.lv.service.bean.GoodsItemBean
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.ItemCompanyCategoryRightGoodsBinding


/**
 * 作者： ygx
 * 创建日期：2022/6/24 8:10 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 *
 */
class CompanyCategoryRightAdapter(private val datas: MutableList<GoodsItemBean>?) :
    BaseQuickAdapter<GoodsItemBean, DataBindBaseViewHolder>
        (R.layout.item_company_category_right_goods, datas) {

    override fun convert(holder: DataBindBaseViewHolder, item: GoodsItemBean) {
        val mBinding: ItemCompanyCategoryRightGoodsBinding =
            holder.getBind() as ItemCompanyCategoryRightGoodsBinding
        mBinding.itemRoot = item
//        //防止列表闪烁
        mBinding.executePendingBindings()
        val layoutPosition = holder.layoutPosition
        mBinding.llHeader.visibility = if (isItemHeader(layoutPosition)) View.VISIBLE else View.GONE
//        val url = item.image?.urlAddBorder(
//            item.backdrop,
//            SizeUtils.dp2px(84.0f), 80
//        )
        mBinding.ivImage.LoadSrc(item.image ?: "")
        val price = item.price ?: 0.0
        if (price == 0.0) {
            mBinding.tvUnit.visibility = View.GONE
            mBinding.tvPrice.text = "面议"
        } else {
            mBinding.tvUnit.visibility = View.VISIBLE
            val priceStr = MoneyUtil.moneydiv("$price", "100")
            mBinding.tvPrice.text = priceStr
        }
        mBinding.clContainers.setOnClickListener {
            onViewItemClickListener?.onClick(layoutPosition, item)
        }
        mBinding.llHeader.setOnClickListener {
            onViewTitleClickListener?.onClick(
                layoutPosition,
                item
            )
        }

//        val layoutParams = clParent.layoutParams as RecyclerView.LayoutParams
//        if (layoutPosition == 0) {
//            layoutParams.topMargin = SizeUtils.dp2px(14.0f)
//        } else {
//            layoutParams.topMargin = SizeUtils.dp2px(12.0f)
//        }
//        clParent.layoutParams = layoutParams
//        clParent.setOnClickListener { onViewItemClickListener?.onClick(layoutPosition, item) }

    }

    /**
     * 判断position对应的Item是否是组的第一项
     *
     * @param position
     * @return
     */
    fun isItemHeader(position: Int): Boolean {
        return if (position == 0) {
            true
        } else {
            val lastGroupName: Int = datas?.get(position - 1)?.secaid ?: -100
            val currentGroupName: Int = datas?.get(position)?.secaid ?: -100
            //判断上一个数据的组别和下一个数据的组别是否一致，如果不一致则是不同组，也就是为第一项（头部）
            lastGroupName != currentGroupName
        }
    }

    var onViewTitleClickListener: OnPositionTListener<GoodsItemBean>? = null
    var onViewItemClickListener: OnPositionTListener<GoodsItemBean>? = null

}